package com.dayaokeji.rhythmschoolstudent.client.home.course

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.ActivityTemplateMethod
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity
import com.dayaokeji.rhythmschoolstudent.client.home.course.adapter.StudentQAListAdapter
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils
import com.dayaokeji.rhythmschoolstudent.wiget.WrapLinearLayoutManager
import com.dayaokeji.server_api.ApiUtils
import com.dayaokeji.server_api.ServerResponse
import com.dayaokeji.server_api.api.ExamApi
import com.dayaokeji.server_api.domain.Exam
import kotlinx.android.synthetic.main.activity_student_qa_list.*
import retrofit2.Call

class StudentQAListActivity : BaseActivity() , ActivityTemplateMethod {

    companion object {

        private const val REL_ID = "rel_id"
        private const val REL_TYPE = "rel_type";

        fun navigation(context: Context,relId: String,relType: Int) {
            val intent = Intent(context, StudentQAListActivity::class.java)
            intent.putExtra(REL_ID,relId)
            intent.putExtra(REL_TYPE,relType)
            context.startActivity(intent)
        }
    }

    private val examApi by lazy { ApiUtils.getApi(ExamApi::class.java) }
    private val studentQAListAdapter by lazy { StudentQAListAdapter() }

    private var relId: String = ""
    private var relType: Int = -1
    private var listCall: Call<ServerResponse<List<Exam>>>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_student_qa_list)
        toolbar?.let {
            setSupportActionBar(it)
        }
        init()
    }

    override fun init() {
        relId = intent.getStringExtra(REL_ID)
        relType = intent.getIntExtra(REL_TYPE, -1)
        super.init()
        setOnListener()
    }

    override fun setupView() {
        refreshLayout.autoRefresh()
        refreshLayout.setOnRefreshListener {
            reqExamList()
        }
    }

    override fun setupViewData() {
        rvExamList.layoutManager = WrapLinearLayoutManager(this)
        rvExamList.adapter = studentQAListAdapter
        studentQAListAdapter.setEmptyView(R.layout.empty_layout,rvExamList)
    }

    private fun setOnListener() {
        studentQAListAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener{ adapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, i: Int ->
            val exam = adapter.data[i] as Exam
            val isApproved = exam.getStatus() == 4 && exam.getResultStatus() == 2
            StudentQAActivity.navigation(StudentQAListActivity@this,exam.id,exam.name,isApproved)
        }
    }

    private fun reqExamList() {
        listCall = examApi.list(relId, relType)
        listCall?.enqueue(object: ServerCallback<List<Exam>>(){
            override fun onResponse(success: Boolean, response: ServerResponse<List<Exam>>?) {
                if (success) {
                    response?.let {
                        it.body?.let {
                            val examList: ArrayList<Exam> = arrayListOf()
                            for (exam in it) {
                                if (exam.name.contains("【拍照问答】",true)) {
                                    examList.add(exam)
                                }
                            }
                            refreshLayout.finishRefresh()
                            studentQAListAdapter.setNewData(examList)
                        }
                    }
                }
            }
        })
    }
}
